<?php

namespace app\admin\model\yxerp;

use think\Model;

/**
 * Department
 */
class Department extends Model
{
    protected $name = 'yxerp_department';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    protected $append = ["parent_name"];
    // 新增父部门的显示字段
    public function getParentNameAttr($value, $data)
    {
        return $this->getParentDepartment($data['pid']);
    }

    protected function getParentDepartment($parentId)
    {
        if (!$parentId || $parentId <= 0) {
            return '';
        }
        return self::where('id', $parentId)->value('name') ?: '无';
    }

}